﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using FrontRoomCheck.FindRoomMethod;
using FrontRoomCheck.Model;
using FrontRoomCheck.Utiltiy;
using YJKCodeBase.WinForm.BackGroundProgressBar;
using ModelCheckUtility.ModelLayer;

namespace FrontRoomCheck.SpecialUse
{
	public class FrontRoomFind_CmdFrontRoomDoorsCheck : IFrontRoomFind<ICloseCurveAnalays>, IPackerFind<ICloseCurveAnalays>, IBase<ICloseCurveAnalays>
	{
		List<ICloseCurveAnalays> IPackerFind<ICloseCurveAnalays>.FindPackers(Document inputDocumnt, params object[] others)
		{
			this.allLevel = (others[0] as List<Level>);
			ProgressBarManger progressBarManger = new ProgressBarManger(new BackWorkDelegate(this.GetCloseCurveAnalays));
			progressBarManger.ShowFormAndDoWork(StaticRescource.m_hWndRevit);
			if (progressBarManger.IfManualAbsort)
			{
				return null;
			}
			return this.lstallDoorPackers;
		}

		public void GetCloseCurveAnalays()
		{
			FrontRoomFind_CmdFrontRoomDoorsCheck.m_doc = StaticRescource.CmdData.Application.ActiveUIDocument.Document;
			List<Phase> list = new List<Phase>();
			PhaseArray phases = FrontRoomFind_CmdFrontRoomDoorsCheck.m_doc.Phases;
			List<Element> list2 = new List<Element>();
			list2 = Utility.FilterElementFormDoc(StaticRescource.CmdData.Application.ActiveUIDocument, this.allLevel);
			new List<FrontRoomDoorPacker>();
			new List<FrontRoomDoorPacker>();
			new List<BaseDoorPacker>();
			List<Element> list3 = new List<Element>();
			for (int i = 0; i < phases.Size; i++)
			{
                list.Add(phases.get_Item(i));
			}
			if (list2.Count == 0)
			{
				FindFrontRoom_ElementMethod.FindStairRoom(FrontRoomFind_CmdFrontRoomDoorsCheck.m_doc, this.allLevel, list, out this.m_AllPacker, out this.m_StairRoom);
				foreach (FrontRoomPacker frontRoomPacker in this.m_StairRoom)
				{
					list3.Add(frontRoomPacker.ThisRoom);
				}
				this.lstallDoorPackers = CloseCurveAnalyser.GetCloseCurveAnalysis(FrontRoomFind_CmdFrontRoomDoorsCheck.m_doc, list3, this.allLevel, list, true);
				this.lstallDoorPackers = this.FilterOutWallDoor();
				return;
			}
			this.lstallDoorPackers = CloseCurveAnalyser.GetCloseCurveAnalysis(FrontRoomFind_CmdFrontRoomDoorsCheck.m_doc, list2, this.allLevel, list, true);
			this.lstallDoorPackers = this.FilterEauipmentDoor();
			this.lstallDoorPackers = this.FilterOutWallDoor();
		}

		public List<ICloseCurveAnalays> FilterEauipmentDoor()
		{
			List<BaseDoorPacker> list = new List<BaseDoorPacker>();
			List<BaseRoomPacker> list2 = new List<BaseRoomPacker>();
			List<FrontRoomPacker> list3 = new List<FrontRoomPacker>();
			List<ICloseCurveAnalays> list4 = new List<ICloseCurveAnalays>();
			foreach (ICloseCurveAnalays closeCurveAnalays in this.lstallDoorPackers)
			{
				for (int i = 0; i < closeCurveAnalays.GetAllDoors().Count; i++)
				{
					list = closeCurveAnalays.GetAllDoors();
				}
				list2 = closeCurveAnalays.GetNearRooms();
				foreach (BaseRoomPacker baseRoomPacker in list2)
				{
					list3.Add(baseRoomPacker as FrontRoomPacker);
				}
				for (int j = 0; j < list3.Count; j++)
				{
					if (list3[j].LstBaseDoorPacker.Count == 1 && list3[j].LstHostElementPacker.Count == 0)
					{
						list.Remove(list3[j].thisFrontRoomDoor[0]);
					}
				}
				list4.Add(closeCurveAnalays);
			}
			return list4;
		}

		public List<ICloseCurveAnalays> FilterOutWallDoor()
		{
			List<BaseDoorPacker> list = new List<BaseDoorPacker>();
			List<ICloseCurveAnalays> list2 = new List<ICloseCurveAnalays>();
			for (int i = 0; i < this.lstallDoorPackers.Count; i++)
			{
				list = this.lstallDoorPackers[i].GetAllDoors();
				for (int j = 0; j < list.Count; j++)
				{
					if (list[j].LstConctRooms.Count == 1)
					{
						list.Remove(list[j]);
						j--;
					}
				}
				list2.Add(this.lstallDoorPackers[i]);
			}
			return list2;
		}

		private List<ICloseCurveAnalays> m_AllFrontRoom = new List<ICloseCurveAnalays>();

		private List<ICloseCurveAnalays> lstallDoorPackers = new List<ICloseCurveAnalays>();

		private List<Element> frontRoomElems = new List<Element>();

		private List<FrontRoomPacker> m_AllPacker = new List<FrontRoomPacker>();

		private List<FrontRoomPacker> m_StairRoom = new List<FrontRoomPacker>();

		private List<FrontRoomPacker> m_FrontRoomByElement = new List<FrontRoomPacker>();

		public static Document m_doc;

		private List<Level> allLevel;
	}
}
